import { defineStore } from "pinia";

export const useStorageStore = defineStore("storage", {
    state: () => ({
        storage: {},
    }),
    actions: {
        getStorageAll() {
            try {
                const storageInfo = uni.getStorageInfoSync();
                storageInfo.keys.forEach((key) => {
                    try {
                        const value = uni.getStorageSync(key);
                        this.storage[key] = value;
                    } catch (e) {
                        this.storage[key] = null;
                    }
                });
                return this.storage;
            } catch (e) {
                this.storage = {};
            }
        },

        setStorage(key, value) {
            this.storage[key] = value;
            uni.setStorageSync(key, value);
        },
    },
});